<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Validation\Rule;
class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
    
    public function username()
    {
        return 'mobile';
    }
    
    
    
    public function sendsms(Request $request)
    {
        if(isset($request->type) && $request->type=='reset')
        {
            $ruls = [
                'mobile' => [
                    'required',
                    Rule::exists('users')->where(function ($query) use ($request) {
                        $query->where('mobile', $request->mobile);
                    })
                ],
                'code' => 'required|size:5|captcha'
            ];
        }else {
            $ruls = [
                'mobile' => 'required|unique:users|size:11',
                'code' => 'required|size:5|captcha'
            ];
        }
        $request->validate($ruls,[
            'mobile.required' => '手机号必须填写.',
            'mobile.unique' => '手机号已经重复.',
            'code.required' => '验证码必须填写.',
            'code.size' => '验证码长为:size.',
            'code.captcha' => '验证码错误,请点击验证码刷新.',
            'mobile.size' => '手机号长为:size.',
            'mobile.exists' => '不存在用户.'
        ]);
        
        $number = rand(1000, 9999);
        
        //存入验证码
        $key = $request->mobile;
        Cache::put($key, $number, 10);
        
        $res = $this->curlPost('http://smssh1.253.com/msg/send/json', [
            'account'  =>  'N3556817',
			'password' => 'fMpi3bqm8',
			'msg' => urlencode('【名片宝】感谢您注册名片宝，你的验证码是：'.$number.' ，有效时间5分钟。'),
			'phone' => $request->mobile,
			'report' => true
        ]);
        //$res['number'] = Cache::get($request->mobile);
        //$res['code'] = 0;
        return $res;
        
    }
    private function curlPost($url,$postFields){
        $postFields = json_encode($postFields);
        $ch = curl_init ();
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8'
        )
            );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_POST, 1 );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields);
        curl_setopt( $ch, CURLOPT_TIMEOUT,1);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
        $ret = curl_exec ( $ch );
        if (false == $ret) {
            $result = curl_error(  $ch);
        } else {
            $rsp = curl_getinfo( $ch, CURLINFO_HTTP_CODE);
            if (200 != $rsp) {
                $result = "请求状态 ". $rsp . " " . curl_error($ch);
            } else {
                $result = $ret;
            }
        }
        curl_close ( $ch );
        return $result;
    }
}
